<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/crm/include/database/db_connect.php");

class quotation_view extends db_connect {
Public $quotation_id, $quotation_type;

	function __construct($quotation_id, $quotation_type){
		//$connect->quotation_db_connect();
		
		$timezone = "Asia/Calcutta";
		if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
		
		if ($_SESSION["first_name"] == ""){
			$this->fullname = $_SESSION["last_name"];
		}else{
			$this->fullname = $_SESSION["first_name"] . " " . $_SESSION["last_name"];
		}
		
		$this->created_by = $_SESSION['user_id'];
		$this->date_created_Ymd = date("Y-m-d H:i:s");
		$this->date_created_dMY = date("d-M-Y h:i:s A");
		$this->quotation_id = $quotation_id;
		$this->quotation_type = $quotation_type;
		//echo $this->quotation_id . "-" . $this->quotation_type;
	}
	
	function execute_query($query){
		$result = mysql_query($query);
		
		if(!$result) {
	       	echo "<br><b><u>Error in query:</b></u><br>$query<br><br> <b><u>Mysql Error for the above query:</b></u><br>".mysql_error()."<hr>";
	       	exit();
     	}else{
     		return $result;
     	}
	}
	
	function get_specialbenefit($zone_type, $zone_id){
		$return_str = "";
		
		if ($zone_type == 0){
			$zone_table_name = "nation_spl_benifit_zone";
		}else{
			$zone_table_name = "state_spl_benifit_zone";
		}
			
		$query1 = "select short_name from $zone_table_name where id=$zone_id And deleted=0";
		$result1 = $this->execute_query($query1);
		$row1 = mysql_fetch_array($result1);
		$return_str = $row1["short_name"];
			
		return $return_str;	
	}
	
	function get_exemption($exemption_tax_id){
		$return_str = "";
		
		$query2 = "select group_concat(short_name separator ', ') 'all_short_name' from tax_master where id IN($exemption_tax_id) And deleted=0";
		$result2 = $this->execute_query($query2);
		$row2 = mysql_fetch_array($result2);
		$return_str = $row2["all_short_name"];
			
		return $return_str;
	}
	
	function getaccount_details(){
		if (strtoupper($this->quotation_type) == "SIMPLE"){
			$additional_columns = ",
			account_spl_benefit_zone.zone_type 'zone_type',
			account_spl_benefit_zone.economic_zone_id 'economic_zone_id',
			group_concat(distinct quotation_delivery_tax_exemption.exemption_tax_id separator ',') 'exemption_tax_id'";
			
			$additional_left_join = "left join account_spl_benefit_zone on (account_spl_benefit_zone.quotation_id = quotations.id) 
			left join quotation_products_delivery_address on (quotation_products_delivery_address.quotations_id = quotations.id And quotation_products_delivery_address.status=0) 
			left join quotation_delivery_tax_exemption on (quotation_delivery_tax_exemption.quotation_products_delivery_address_id = quotation_products_delivery_address.id)";
		}
		
		
		$query = "select 
		quotations.account_id,
		date_format(quotations.valid_upto, '%D %M, %Y') 'format_valid_upto',
		quotations.payment_terms,
		accounts.name 'customer_name',
		quotation_account.address 'address',
		quotation_account.state 'state',
		quotation_account.city 'city',
		group_concat(distinct quotation_contacts.name separator ', ') 'addressed_to',
		concat(u1.first_name, ' ', u1.last_name) 'on_behalf_of' 
		$additional_columns 
		from quotations 
		left join accounts on (accounts.id = quotations.account_id) 
		left join quotation_account on (quotation_account.quotations_id = quotations.id And quotation_account.status=0) 
		left join quotation_contacts on (quotation_contacts.quotations_id = quotations.id) 
		$additional_left_join 
		left join users u1 on (u1.id=quotations.assigned_to) 
		where quotations.id='" . $this->quotation_id . "'";
		$result = $this->execute_query($query);
		$row = mysql_fetch_array($result);
		$this->format_valid_upto = $row["format_valid_upto"];
		$this->payment_terms = $row["payment_terms"];
		//$this->body_name = htmlentities($row['name']);
		//echo $query;
		
		if (strtoupper($this->quotation_type) == "SIMPLE"){
			$zone_type = $row["zone_type"];
			$zone_id = $row["economic_zone_id"];
			$exemption_tax_id = $row["exemption_tax_id"];
			$special_benefit = "";
			$exemption = "";
			
			if (is_null($zone_id) == false){
				$special_benefit = $this->get_specialbenefit($zone_type, $zone_id);
			}
			
			if (is_null($exemption_tax_id) == false){
			$exemption = $this->get_exemption($exemption_tax_id);
			}
		}
		
		$return_str = "";
		
		$return_str = "<div class='subs'>
        <p class='view_heads'>Account Details</p>
        <table width='100%' align='center' cellpadding='5' cellspacing='0' border='1' bordercolor='#ccc' style='border-collapse:collapse; border:1px solid #ccc; margin-bottom:10px;'>
                    	<tbody>              
                        	<tr valign='top'>
        						<td class='head' width='200'>Customer name:</td><td width='400'><span>" . htmlentities($row['customer_name']) . "</span></td><td width='100' class='head'>State:</td><td><span>" . htmlentities($row['state']) . "</span></td>
							</tr>
                            <tr valign='top'>
        						<td class='head' width='200'>Address:</td><td width='400'><span>" . htmlentities($row['address']) . "</span></td><td width='100' class='head'>City: </td><td><span>" . htmlentities($row['city']) . "</span></td>
							</tr>
                            <tr valign='top'>
        						<td class='head' width='200'>Quotation addressed to:</td><td colspan='3'><span>" . htmlentities($row['addressed_to']) . "</span></td>
							</tr>";
		
		if (strtoupper($this->quotation_type) == "SIMPLE"){
			$return_str .= "<tr valign='top'>
        						<td class='head' width='200'>Special Benefits:</td><td width='400'><span>" . htmlentities($special_benefit) . "</span></td><td width='100' class='head'>Exemption:</td><td><span>" . htmlentities($exemption) . "</span></td>
							</tr>";
		}
		
		$return_str .= "<tr valign='top'>
        						<td class='head' width='100'>Quotation on behalf of:</td><td colspan='3'><span>" . htmlentities($row['on_behalf_of']) . "</span></td>
							</tr>
						</tbody>
        </table>         
        </div>";
		
		return $return_str;
	}
	
	function get_tc($heading, $tablename, $where_query, $additional_tablename, $additional_where_query, $payment_terms){
		$return_str = "";
		$count = 1;
		
		for ($i=0;$i<=1;$i++){
			if ($i == 0){
				$query = "select term_name, term_condition from $tablename where $where_query";
			}else{
				$query = "select term_name, term_condition from $additional_tablename where $additional_where_query";
			}
			
			$result = $this->execute_query($query);
			
			while ($row = mysql_fetch_array($result)){
				
				if ($count == 1){
					$return_str .= "<p class='classify'>$heading</p>$payment_terms
	        		<table width='100%' align='center' cellpadding='5' cellspacing='0' border='1' bordercolor='#ccc' style='border-collapse:collapse;' id='ptac'>
	                <tbody>
	                <tr class='head' valign='top' style='font-weight:bold;'>
	                <td width='60'>S/N</td>
	                <td width='190'>Term Name</td>
	                <td colspan='3'>Terms Conditions</td>
	                </tr>";
				}
				
				$return_str .=  "<tr valign='top'>
	                <td>$count</td>
	                <td>" . htmlentities($row["term_name"]) . "</td>
	                <td colspan='3'>" . $row["term_condition"] . "</td>
	                </tr>";
				
				$count = $count + 1;
			}	
		}
		
		$return_str .= "</tbody></table>";
		return $return_str;
	}
	
	function getterms_conditions(){
		$return_str = "<div class='subs'>";
		$return_str .= "<p class='view_heads'>Terms and Conditions</p>
        <p>Valid Date: <span>" . $this->format_valid_upto . "</span></p>";
		
		$heading = "Principal terms and conditions";
		$tablename = "quotation_principal_t_c";
		$additional_tablename = "quotation_t_c_additional";
		$where_query = "quotations_id='" . $this->quotation_id . "' And status=0";
		$additional_where_query = "quotations_id='" . $this->quotation_id . "' And status=0 And type=3";
		$payment_terms = "";
		$return_str .= $this->get_tc($heading, $tablename, $where_query, $additional_tablename, $additional_where_query, $payment_terms);
		
		$heading = "Commercial terms and conditions";
		$tablename = "quotation_t_c";
		$additional_tablename = "quotation_t_c_additional";
		$where_query = "quotations_id='" . $this->quotation_id . "' And status=0 And type=2";
		$additional_where_query = "quotations_id='" . $this->quotation_id . "' And status=0 And type=2";
		$payment_terms = "<p>Payment terms: <span>" . $this->payment_terms . "</span></p>";
		$return_str .= $this->get_tc($heading, $tablename, $where_query, $additional_tablename, $additional_where_query, $payment_terms);
		
		$heading = "Standard terms and conditions";
		$tablename = "quotation_t_c";
		$additional_tablename = "quotation_t_c_additional";
		$where_query = "quotations_id='" . $this->quotation_id . "' And status=0 And type=1";
		$additional_where_query = "quotations_id='" . $this->quotation_id . "' And status=0 And type=1";
		$payment_terms = "";
		$return_str .= $this->get_tc($heading, $tablename, $where_query, $additional_tablename, $additional_where_query, $payment_terms);
		
		$return_str .= "</div>";
		return $return_str;
	}
	
	function get_annexures($heading, $tablename, $where_query){
		$return_str = "";
		$count = 1;
		
		$query = "select description from $tablename where $where_query";
		$result = $this->execute_query($query);
			
		while ($row = mysql_fetch_array($result)){
				
			if ($count == 1){
				$return_str .= "<p class='classify'>$heading</p><ul>";
			}
				
			$return_str .=  "<li>" . $row["description"] . "</li>";
			$count = $count + 1;
		}	
		
		$return_str .= "</ul>";
		return $return_str;
	}
	
	function getannexures(){
		$return_str = "<div class='subs'>";
		$return_str .= "<p class='view_heads'>Annexures</p>";
		
		$heading = "Include about Softcell";
		$tablename = "quotation_about_softcell";
		$where_query = "quotations_id='" . $this->quotation_id . "' And status=0";
		$return_str .= $this->get_annexures($heading, $tablename, $where_query);
		
		$heading = "Include Generic Product / Solution Write - up";
		$tablename = "quotation_generic_writeup";
		$where_query = "quotations_id='" . $this->quotation_id . "' And status=0";
		$return_str .= $this->get_annexures($heading, $tablename, $where_query);
		
		$heading = "Include Principal / Product Write - up";
		$tablename = "quotation_principal_writeup";
		$where_query = "quotations_id='" . $this->quotation_id . "' And status=0";
		$return_str .= $this->get_annexures($heading, $tablename, $where_query);
		
		$return_str .= "</div>";
		return $return_str;
	}
	
	function get_emailids($tablename, $where_query){
		$return_str = "";
		
		$query = "select group_concat(distinct email_id separator ', ') 'emailids' from $tablename where $where_query";
		$result = $this->execute_query($query);
		$row = mysql_fetch_array($result);
		$return_str = $row["emailids"];
		
		return $return_str;
	}
	
	function getemail(){
		$return_str = "<div class='subs'>";
		$return_str .= "<p class='view_heads'>Email</p>";
		
		$tablename = "quotation_email_to";
		$where_query = "quotations_id='" . $this->quotation_id . "'";
		$to_emailids = $this->get_emailids($tablename, $where_query);
		
		$tablename = "quotation_email_cc";
		$cc_emailids = $this->get_emailids($tablename, $where_query);
		
		$where_query = "quotations_id='" . $this->quotation_id . "' And status=0";
		$query = "select subject, body, signature from quotation_email where $where_query";
		$result = $this->execute_query($query);
		$row = mysql_fetch_array($result);
		$mail_subject = $row["subject"];
		$mail_body = $row["body"];
		$mail_signature = $row["signature"];
		
		$return_str .= "<table width='100%' align='center' cellpadding='5' cellspacing='0' border='1' bordercolor='#ccc' style='border-collapse:collapse; border:1px solid #ccc; margin-bottom:10px;'>
                    	<tbody>              
                        	<tr valign='top'>
        						<td class='head' width='100'>To:</td><td><span>$to_emailids</span></td>
							</tr>
                            <tr valign='top'>
        						<td class='head' width='100'>CC:</td><td><span>$cc_emailids</span></td>
							</tr>
                            <tr valign='top'>
        						<td class='head' width='100'>Subject:</td><td><span>$mail_subject</span></td>
							</tr>
                            <tr valign='top'>
        						<td class='head' width='100'>Body:</td><td><span>$mail_body</span></td>
							</tr>
                            <tr valign='top'>
        						<td class='head' width='100'>Signature Message:</td><td><span>$mail_signature</span></td>
							</tr>
						</tbody>
                        </table>";
		$return_str .= "</div>";
		return $return_str;
	}
	
	function getproducts(){
		$return_str = "";
		
		$where_query = "quotations_id='" . $this->quotation_id . "' And status=0 order by sr_no Asc";
		$query = "select id, sr_no, description, type, qty, rate, qty*rate 'amount' from quotation_products where $where_query";
		$result = $this->execute_query($query);
			
		while ($row = mysql_fetch_array($result)){
			$return_str .= "<tr class='items blank'>
                            	<td><div class='num'>" . $row["sr_no"] . "</div></td>
                            	<td>" . $row["description"] . "</td>
                                <td  width='150'>" . $row["type"] . "</td>
                                <td align='right'>" . $row["qty"] . "</td>
                                <td align='right'>Rs." . $this->fpfGetIndianFormat($row["rate"]) . "</td>
                                <td align='right'>Rs." . $this->fpfGetIndianFormat($row["amount"]) . "</td>                              	
                            </tr>";
			
			if (strtoupper($this->quotation_type) == "COMPLEX"){
				$quotation_product_id = $row["id"];
				$where_query1 = "quotations_id='" . $this->quotation_id . "' And quotation_products_id='$quotation_product_id' And status=0";
				$query1 = "select address, softcell_bill_location, state, city, spl_benefits, special_benefit_zone_type from quotation_products_delivery_address where $where_query1";
				$result1 = $this->execute_query($query1);
				$row1 = mysql_fetch_array($result1);
			
				$zone_type = $row1["special_benefit_zone_type"];
				$zone_id = $row1["spl_benefits"];
				$special_benefit = "";
				
				if (is_null($zone_id) == false){
				$special_benefit = $this->get_specialbenefit($zone_type, $zone_id);
				}
				
				$where_query2 = "quotations_id='" . $this->quotation_id . "' And quotation_products_id='$quotation_product_id' And status=0";
				$query2 = "select group_concat(distinct tax_master.short_name separator ', ') 'all_exemption' from quotation_products_delivery_address left join quotation_delivery_tax_exemption on (quotation_delivery_tax_exemption.quotation_products_delivery_address_id = quotation_products_delivery_address.id) left join tax_master on (tax_master.id In(quotation_delivery_tax_exemption.exemption_tax_id) And tax_master.deleted=0) where $where_query2";
				$result2 = $this->execute_query($query2);
				$row2 = mysql_fetch_array($result2);
				$exemption = $row2["all_exemption"];
				
				$return_str .= "<tr class='blank cols'>
                            	<td></td>
                                <td>
                                	Shipping address:
                                    <span>" . $row1["address"] . "</span>
                                </td>
                                <td>
                                	From Softcell Branch: <span>" . $row1["softcell_bill_location"] . "</span>
                                </td>
                                <td>
                                	State: <span>" . $row1["state"] . "</span>
                                </td>
                                <td>
                                	City: <span>" . $row1["city"] . "</span>
								</td>
                                <td>
                                	Special Benefit: <span>" . $special_benefit . "</span>
                                    Exemption: <span>" . $exemption . "</span>
								</td>                              	
                            </tr>";
			}		
		}
		
		return $return_str;
	}
	
	function getproduct_details(){
		$return_str = "<div class='subs'>";
		$return_str .= "<p class='view_heads'>Product Details</p>";
		$return_str .= "<p class='classify'>From Opportunity</p>";
		$return_str .= "<table width='100%' align='center' cellpadding='5' cellspacing='0' border='1' bordercolor='#ccc' style='border-collapse:collapse; border:1px solid #ccc; margin-bottom:10px;'>
                        <tbody>
                        	<tr valign='middle' class='head'>
                            	<td>S/N</td>
                            	 <td align='center'>Description</td>
                                 <td align='center'>Type</td>
                    	        <td align='center'>Quantity</td>
                        	    <td align='center'>Unit Rate</td>
                            	<td align='center'>Amount</td>   
                            </tr>";
		
		$return_str .= $this->getproducts();
		
		$return_str .= "</tbody></table></div>";
		return $return_str;
	}
	
	function getproducts_summary(){
		$return_str = "";
		$total_rate = 0;
		$total_amount = 0;
		$total_costprice = 0;
		$total_cst = 0;
		$total_vat = 0;
		$total_service = 0;
		$total_lbt = 0;
		$total_octroi = 0;
		$total_wct = 0;
		$total_extended = 0;
		
		$where_query = "quotation_products.quotations_id='" . $this->quotation_id . "' And quotation_products.status=0 order by quotation_products.sr_no Asc";
		$query = "select quotation_products.id, quotation_products.sr_no, quotation_products.product_code, quotation_products.description, quotation_products.type, quotation_products.qty, quotation_products.rate, quotation_products.qty*quotation_products.rate 'amount',quotation_products.cost_price, quotation_products_delivery_address.softcell_bill_location 'shippinglocation' from quotation_products 
		left join quotation_products_delivery_address on (quotation_products_delivery_address.quotations_id = quotation_products.quotations_id And quotation_products_delivery_address.quotation_products_id = quotation_products.id And quotation_products_delivery_address.status=0)  
		where $where_query";
		$result = $this->execute_query($query);
			
		while ($row = mysql_fetch_array($result)){
			$quotation_product_id = $row["id"];
			$cst_value = "-";
			$vat_value = "-";
			$stax_value = "-";
			$lbt_value = "-";
			$octroi_value = "-";
			$wct_value = "-";
			$current_tax_total = 0;
			
			$query1 = "select quotation_products_tax.value 'appliedtaxvalue', tax_master.short_name 'appliedtax' from quotation_products_tax 
			left join tax_master on (tax_master.id = quotation_products_tax.tax_master_id And tax_master.deleted = 0)
			where quotation_products_tax.quotation_products_id='" . $quotation_product_id . "'";
			$result1 = $this->execute_query($query1);
			//echo $query1 . "<br><br>";
			
			while ($row1 = mysql_fetch_array($result1)){
				$appliedtax = $row1["appliedtax"];
				$appliedtaxvalue = $row1["appliedtaxvalue"];
				
				if (strtoupper($appliedtax) == "CST"){
					$cst_value = "Rs." . $this->fpfGetIndianFormat($appliedtaxvalue);
					$total_cst += $appliedtaxvalue;
				}elseif (strtoupper($appliedtax) == "VAT"){
					$vat_value = "Rs." . $this->fpfGetIndianFormat($appliedtaxvalue);
					$total_vat += $appliedtaxvalue;
				}elseif (strtoupper($appliedtax) == "STAX"){
					$stax_value = "Rs." . $this->fpfGetIndianFormat($appliedtaxvalue);
					$total_service += $appliedtaxvalue;
				}elseif (strtoupper($appliedtax) == "LBT"){
					$lbt_value = "Rs." . $this->fpfGetIndianFormat($appliedtaxvalue);
					$total_lbt += $appliedtaxvalue;
				}elseif (strtoupper($appliedtax) == "OCTRO"){
					$octroi_value = "Rs." . $this->fpfGetIndianFormat($appliedtaxvalue);
					$total_octroi += $appliedtaxvalue;
				}elseif (strtoupper($appliedtax) == "WCT"){
					$wct_value = "Rs." . $this->fpfGetIndianFormat($appliedtaxvalue);
					$total_wct += $appliedtaxvalue;
				}
				
				$current_tax_total += $appliedtaxvalue;
			}
			
			$return_str .= "<tr valign='top' class='bg'>
                            	<td>" . $row["product_code"] . "</td>
                                <td>" . $row["description"] . "</td>
                                <td>" . $row["type"] . "</td>
                                <td align='right'>" . $row["qty"] . "</td>
                                <td align='right'>Rs." . $this->fpfGetIndianFormat($row["rate"]) . "</td>
                                <td align='right'>Rs." . $this->fpfGetIndianFormat($row["amount"]) . "</td>
                                <td align='right'>Rs." . $this->fpfGetIndianFormat($row["cost_price"]) . "</td>
                                <td>" . $row["shippinglocation"] . "</td>
                                <td align='right'>$cst_value</td>
                                <td align='right'>$vat_value</td>
                                <td align='right'>$stax_value</td>
                                <td align='right'>$lbt_value</td>
                                <td align='right'>$octroi_value</td>
                                <td align='right'>$wct_value</td>
                                <td align='right'>Rs." . $this->fpfGetIndianFormat($row["amount"] + $current_tax_total) . "</td>
                            </tr>";
			
			$total_rate += $row["rate"];
			$total_amount += $row["amount"];
			$total_costprice += $row["cost_price"];
			$total_extended += $row["amount"] + $current_tax_total;
		}
		
		$profit_loss_amount = $total_extended - $total_costprice;
		
		$return_str .= "<tr valign='top' class='bolds'>                            	
                                <td colspan='4'>Total</td>                                                        
                                <td align='right'>Rs." . $this->fpfGetIndianFormat($total_rate) . "</td>
                                <td align='right'>Rs." . $this->fpfGetIndianFormat($total_amount) . "</td>
                                <td align='right'>Rs." . $this->fpfGetIndianFormat($total_costprice) . "</td>
                                <td></td>
                                <td align='right'>" . ($total_cst > 0 ? "Rs." . $this->fpfGetIndianFormat($total_cst) : "-") . "</td>
                                <td align='right'>" . ($total_vat > 0 ? "Rs." . $this->fpfGetIndianFormat($total_vat) : "-") . "</td>
                                <td align='right'>" . ($total_service > 0 ? "Rs." . $this->fpfGetIndianFormat($total_service) : "-") . "</td>
                                <td align='right'>" . ($total_lbt > 0 ? "Rs." . $this->fpfGetIndianFormat($total_lbt) : "-") . "</td>
                                <td align='right'>" . ($total_octroi > 0 ? "Rs." . $this->fpfGetIndianFormat($total_octroi) : "-") . "</td>
                                <td align='right'>" . ($total_wct > 0 ? "Rs." . $this->fpfGetIndianFormat($total_wct) : "-") . "</td>
                                <td align='right'>Rs." . $this->fpfGetIndianFormat($total_extended) . "</td>
                            </tr>";
		
		$return_str .= "<tr valign='top' class='gtotal'>
                            	<td align='right' colspan='14'>Grand Total</td>
                                <td align='right'>Rs." . $this->fpfGetIndianFormat($total_extended) . "</td>
                            </tr>";
		
		$return_str .= "<tr valign='top' class='bolds'>
                            	<td align='right' colspan='14'>Profit / Loss<br /><span style='font-weight:normal;'>(Total amount - Cost)</span></td>
                                <td align='right'>Rs." . $this->fpfGetIndianFormat($profit_loss_amount) . "</td>
                            </tr>";
		
		return $return_str;
	}
	
	function getquotation_summary(){
		$return_str = "<div class='subs'>";
		$return_str .= "<p class='view_heads'>Quotation Summary</p>";
		$return_str .= "<table width='100%' align='center' cellpadding='5' cellspacing='0' border='1' bordercolor='#ccc' style='border-collapse:collapse; border:1px solid #ccc; margin-bottom:10px;'>
                    	<tbody>
                        	<tr valign='top' class='head'>
                            	<td align='center'>Product Code</td>
                	            <td align='center'>Description</td>
                                <td align='center'>Type</td>
                    	        <td align='center'>Quantity</td>
                        	    <td align='center'>Rate</td>
                            	<td align='center'>Total Amount</td>  
                                <td align='center'>Cost</td> 
                                <td align='center'>Shipping Location</td>
                                <td align='center'>CST</td>  
                                <td align='center'>VAT</td>  
                                <td align='center'>Service Tax</td>   
                                <td align='center'>LBT</td>  
                                <td align='center'>Octroi</td>
                                <td align='center'>WCT</td>  
                                <td align='center'>Extended Amount</td>  
                            </tr>";
		$return_str .= $this->getproducts_summary();
		
		$return_str .= "</tbody></table></div>";
		return $return_str;
	}
	
	function fpfGetIndianFormat($value){
		$is_minus = "No";
		
		if ($value < 0){
			$is_minus = "Yes";
			$value = abs($value);
		}
		
		//echo "<hr>" . $value;
		
		$n=$value;
		$fv=explode(".",$value);
		$n=$fv[0];
		
		$len = strlen($n); 
		$hundreds = substr($n,$len-3,3); 
		$thousands = substr($n,0,-3); 
	
		if ($len < 3){
			$finalValue = $n;
		}else if($this->checkNum(strlen($thousands))==FALSE){
			for($i=0;$i<strlen($thousands);$i+=2){
			$tmp.=substr($thousands, $i, 2).',';
			}
		
			$finalValue=substr($tmp, 0, strlen($tmp)-1); 
			
			if($finalValue==""){
				//$finalValue=$hundreds.".00";
				$finalValue=$hundreds;
			}else{
				//$finalValue=$finalValue.",".$hundreds.".00";
				$finalValue=$finalValue.",".$hundreds;
			}
		}else{
			if(strlen($thousands)>1){
				$lsF=substr($thousands,1);
				$lsF1=substr($thousands,0,1);
			
				for($i=0;$i<strlen($lsF);$i+=2){
					$tmp.=substr($lsF, $i, 2).',';
				}
				
				$finalValue=substr($tmp, 0, strlen($tmp)-1); 
				$finalValue=$lsF1.",".$finalValue;
				//$finalValue=$finalValue.",".$hundreds.".00";
				$finalValue=$finalValue.",".$hundreds;
			}else{
				$lsF1=substr($thousands,0,1);
				//$finalValue=$lsF1.",".$hundreds.".00";
				$finalValue=$lsF1.",".$hundreds;
			}
		}
	
		//echo " -- " . $finalValue;
		
		if ($fv[1] == ""){
			$returnvalue = $finalValue . ".00";
		}else{
			if (strlen($fv[1]) == 1){
				$returnvalue = $finalValue . "." . $fv[1] . "0";
			}else{
				$returnvalue = $finalValue . "." . $fv[1];
			}	
		}
		
		if ($is_minus == "No"){
			//echo " -- " . $returnvalue;
			return $returnvalue;
		}else{
			//echo "-" . $returnvalue;
			return "-" . $returnvalue;
		}
	}
		
	function checkNum($num){
		return ($num%2) ? TRUE : FALSE;
	}
	
	function getContactDetails() {
		$this->db_connect();
        $data = array();
        
        $query = "SELECT SQL_CALC_FOUND_ROWS users.email1 email_id
                     FROM users
                     LEFT JOIN users t1 on t1.id = users.reports_to_id and t1.deleted=0
                     WHERE  users.deleted='0' AND users.status='active'
                     AND users.email1 IS NOT NULL AND TRIM(users.email1) !=''
                     ORDER BY users.user_name ASC";
        
        $queryRes = mysql_query($query) or die("123123");
        while ($record = mysql_fetch_assoc($queryRes)) {
                //array_push($data, $record['email_id']);
                array_push($data, "{id:'" . $record['email_id'] . "',text:'" . $record['email_id'] . "'}");
            }
            
        
        
        return $data;
    }
}

?>